package com.augurit.wf.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.wf.common.entity.Jbpm4Execution;

@Repository
public class Jbpm4ExecutionDao extends BaseDao<Jbpm4Execution, Long> {
	
	public List<Jbpm4Execution> getJbpm4ExecutionByProcInstDbid(Long processInstanceDbid){
		List<Jbpm4Execution> list = null;
		if(processInstanceDbid != null){
			String hql = "from Jbpm4Execution ps where ps.processInstanceId="+processInstanceDbid ;
			list = this.find(hql);
		}
		return list;
	}
	
	
	public List<Jbpm4Execution> getJbpm4ExecutionByProInstId(String procInstId){
		List<Jbpm4Execution> list = null;
		if(procInstId != null && procInstId.trim().length()>0){
			String hql = "select ps from Jbpm4Execution ps,Jbpm4HistProcinst procInst " +
					"where ps.processInstanceId=procInst.procInstDbId and  procInst.procInstId='" + procInstId + "'";
			list = this.find(hql);
		}
		return list;
	}
	
	
	/**
	 * 修改流程实例以及流程实例下的所有execution的状态
	 * @param id
	 * @param state
	 */
	public void updateStateByProcInstId(String procInstId,String state){
		if(procInstId != null && procInstId.trim().length()>0 &&
				state != null && state.trim().length()>0){
			List<Jbpm4Execution> list = getJbpm4ExecutionByProInstId(procInstId);
			if(list != null && list .size()>0){
				for(Jbpm4Execution entity :list){
					entity.setState(state);
					this.save(entity);
				}
			}
		}
	}
	
}